package com.bookseek.xreader.utils;

import android.util.Log;

import com.bookseek.xreader.consts.XreaderConts;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class MiniDataUtils {
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";

    private static String SHUDI_AES_KEY = "1234567890qazxsw";

    //public static Pattern JSONP_PA = Pattern.compile("(?<leftText>.*?')::::(?<text>.*?)::::(?<rightText>.*?[)])");
    public static Pattern JSONP_PA = Pattern.compile("::::(?<text>.*?)::::");

    public static String aesEncryptCBCWithIvToBase64SafeUrlStr(String srcText) {
        String encrypt = null;
        try {
            encrypt = encrypt(srcText,SHUDI_AES_KEY,SHUDI_AES_KEY);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("MiniData",e.getMessage());
            return "";
        }
        encrypt = encrypt
                .replaceAll("=","@")
                .replaceAll("[+]","-")
                .replaceAll("/","_");
        return encrypt;
    }
    public static String aesDecryptCBCWithIvFromBase64SafeUrlStr(String encryptText){
        encryptText = encryptText.replaceAll("@","=")
                .replaceAll("-","+")
                .replaceAll("_","/");
        try {
            return decrypt(encryptText,SHUDI_AES_KEY,SHUDI_AES_KEY);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("MiniData",e.getMessage());
        }
        return "";
    }

    public static String encrypt(String plainText, String key, String iv) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String cipherText, String key, String iv) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decodedBytes = Base64.getDecoder().decode(cipherText);
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
    public static String aesDecryptCBCWithIvFromBase64SafeUrlJSONP(String srcRespText){
        Integer kbSize = srcRespText.getBytes().length/1024;
        //String leftText = JkhlUtils.extractFirstResultListByGroupNames(srcRespText,JSONP_PA,"leftText");
        String text = JkhlUtils.matchOneGroup(srcRespText,JSONP_PA,"text");
        //DLog.info("jsonp Text=",text);
        if(JkhlUtils.isBlank(text)){
            Log.i(XreaderConts.TAG,"未提取到JSONP TEXT");
            text = srcRespText;
        }
        String rawText =  aesDecryptCBCWithIvFromBase64SafeUrlStr(text);
        return rawText;
    }



}
